﻿using System;
using System.Collections.Generic;
using Authority.Logic;
using Common.Library;
using Container.Library;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Project.Logic;
using Project.Model;
using Tool.Model;
using Web.Manage.App_Start;

namespace Web.Manage.Areas.Staff.Controllers
{
    /// <summary>
    /// 员工入职操作控制器
    /// </summary>
    [Area("Staff")]
    public class EmployeeEntryController : BaseController
    {
        private IHostingEnvironment hostingEnv;
        private readonly IEmployeeService employeeService = null;//员工管理逻辑
        public EmployeeEntryController(IHostingEnvironment env)
        {
            this.hostingEnv = env;
            //员工管理逻辑注入
            employeeService = UnityCIContainer.Instance.GetService<IEmployeeService>();
        }

        /// <summary>
        /// 列表界面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyView)]
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        ///新增界面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult Add()
        {
            return View();
        }

        /// <summary>
        /// 编辑界面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult Modify()
        {
            return View();
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult UploadImg()
        {
            return View();
        }

        #region 查询
        /// <summary>
        /// 获取员工分页列表
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult LoadEmployeeList()
        {
            var resultInfo = new ResultJsonInfo<List<T_EmployeeEntity>>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<EmployeeInfoQueryRequest>>();
              
                resultInfo = employeeService.LoadEmployeeList(parametersInfo);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }


        /// <summary>
        /// 获取员工图片信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult LoadEmployeeImg()
        {
            var resultInfo = new ResultJsonInfo<T_EmployeeImgEntity>();
            try
            {
                var parametersInfo = DeserializeRequestData<ParametersInfo<string>>();

                resultInfo = employeeService.LoadEmployeeImg(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        #endregion

        #region 更新操作

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        [EnableCors("AllowSpecificOrigin")]
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult UploadImage()
        {
            var resultInfo = new ResultJsonInfo<string>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<Dictionary<string, object>>>();

                resultInfo = employeeService.UploadImage(hostingEnv.WebRootPath, parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 增加信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult AddInfo()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<EmployeeOperateRequest>>();

                resultInfo = employeeService.Save(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }


        /// <summary>
        ///修改信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult ModifyInfo()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<EmployeeOperateRequest>>();

                resultInfo = employeeService.Save(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }


        #endregion

        #region 删除信息
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult Remove()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<List<int>>>();

                resultInfo = employeeService.Remove(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }
        #endregion
    }
}